<template>
	<view class="content">
		<image class="logo" src="./image/logo.png"></image>
		<view class="body">
			<h1 style="width: 100%; text-align: center; margin-bottom: 50rpx;">医院门诊系统</h1>
			<text>手机号</text>
			<el-input v-model="account" style="margin-bottom:50rpx ;" placeholder="输入手机号"></el-input>
			<text>密码</text>
			<el-input v-model="password" style="margin-bottom:50rpx ;" type="password" placeholder="输入密码"></el-input>
			<view style="width: 40%; margin-bottom: 20rpx;">
				<el-select v-model="type" placeholder="选择角色" size="small">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</view>
			<el-button @click="login" type="primary">登录</el-button>
			<view style="display: flex; flex-direction: row;" @click="goRegister">
				<h6 style="width: 100%; text-align: center; margin-bottom: 50rpx;">没有账号？去注册</h6>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import axiosInstance from '@/Config/axiosConfig'
	import globalStore from '@/store/GlobalStore';
	import Emessage from '@/Utils/Message';
	//手机号
	const account = ref("");
	//密码
	const password = ref("");
	//角色
	const type = ref("");
	const options = ref([
		{
			label: "患者",
			value: "PATIENT"
		},
		{
			label: "医生",
			value: "DOCTOR"
		},
		{
			label: "窗口",
			value: "window"
		},
		{
			label: "药房",
			value: "CACHIER"
		}
	])
	//登录
	const login = async () => {
		try {
			let temp=type.value;
			if(type.value!="PATIENT"){
				temp="DOCTOR"
				}
			const response = await axiosInstance.post('/user/login', {
				account: account.value,
				password: password.value,
				type: temp
			})
			if (response.data.code == 200) {
				Emessage("登录成功", "success");
				account.value = ""
				password.value = ""
				globalStore.id = response.data.data.id;
				globalStore.token = response.data.data.token;
uni.setStorageSync('id',globalStore.id);
				if (type.value == "PATIENT") {
					globalStore.patientid = globalStore.id;
					uni.navigateTo({ url: '/pages/Home/Home' })
				}
				else if (type.value == "DOCTOR") {
					uni.navigateTo({ url: '/pages/doctor/queue' })
				}else if(type.value=="window"){
					uni.navigateTo({ url: '/pages/windowRegister/windowRegister' })
				}else if(type.value=="CACHIER"){
					uni.navigateTo({ url: '/pages/Cachier/Cachier' })
				}
				type.value = "选择角色";

			} else if (response.data.code == 300) {
				Emessage("登录成功", "success");
				account.value = ""
				password.value = ""
				globalStore.id = response.data.data.id;
				globalStore.token = response.data.data.token;
				globalStore.patientid = globalStore.id;
				uni.navigateTo({ url: '/pages/index/registerInf' })
			}
			else {
				type.value = "选择角色";
				Emessage(response.data.message, "error")
			}
		} catch (e) {
			type.value = "选择角色";
			Emessage("没访问成功", "warning")
		}
	}
	//注册
	const goRegister = async () => {
		uni.navigateTo({
			url: '/pages/Register/Register',
			success: res => { },
			fail: () => { },
			complete: () => { }
		});
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: start;
	}

	.logo {
		height: 400rpx;
		width: 100%;
		margin-bottom: 50rpx;

	}

	.body {
		display: flex;
		justify-content: center;
		flex-direction: column;
		width: 70%;
		height: 70%;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>